/**
 * Created by LeungZ on 2016/10/17.
 */
class Deployment {
    constructor($scope, ctgNotification, nodeSrv, appSrv, $state) {
        "ngInject";
        $scope.$parent.vm.step = 1;
        this._notification = ctgNotification;
        this._fData = $scope.$parent.vm.fData;
        this._appSrv = appSrv;
        this._state = $state;

        this.rolVerId = $state.params.rolVerId;
        this.appImgList = $scope.$parent.vm.appImgList;
        this.groupList = [];

        if (this._fData.services) {
            this.serviceId = this._fData.services[0].serviceId;
            this.versionId = this._fData.services[0].versionId;
            this.baseCtrl = this._fData.services[0].baseCtrl;
            this.advCtrl = this._fData.services[0].advCtrl;
            
        }

        if (this._fData.cluster) {
            nodeSrv.getGroupList({perPage: 99, page: 1, code: this._fData.cluster.clusterCode})
                .then(data=> {
                    this.groupList = data.list;
                }).catch(data=> {
                    this._notification.error('获取宿主机组详情数据有误');
                })
        }
    }

    checkServiceValidate() {
        if (this.baseCtrl.podAppName) {
            this.serviceCodeChecking = true;
            this._appSrv.checkServiceCode({
                appId: this._fData.app.id,
                serviceCode: this.baseCtrl.podAppName
            }).then(data=> {
                this.baseCtrl.baseForm.podAppName.$setValidity('duplicate', !data.exist);
                this.baseCtrl.baseForm.podAppName.$setValidity('network', true);
            }).catch(data=> {
                this.baseCtrl.baseForm.podAppName.$setValidity('network', false);
            }).finally(()=> {
                this.serviceCodeChecking = false;
            });
        }
    }

    next() {
        if (this.serviceCodeChecking || this.baseCtrl.baseForm.$invalid ||
            (this.advCtrl.autoScaleForm && this.advCtrl.autoScaleForm.$invalid) || (this.advCtrl.domainForm && this.advCtrl.domainForm.$invalid)) return;

        this.loading = true;
        this.namespace=this._state.params.namespace;
        this.clusterCode=this._state.params.k8sCode;
        this._fData.app.namespace=this.namespace;
        this._fData.services = [this];
        this._appSrv.getYaml([this]).then(data=> {
            this._fData.yJsonList = data;
            this._state.go('app-service-version-publish.release');
        }).catch((data)=> {
             if(data.resultCode==1200){
                this._notification.error(data.resultMsg);
            }else{
            this._notification.error("加载Yaml配置失败.");
            }
        }).finally(()=> {
            this.loading = false;
        })

    }
}
export default Deployment